﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class VehicleController : Controller
    {

        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();
      
        //
        // GET: /Admin/Vehicle/

        public ActionResult Index()
        {
            List<VehicleInformation> ver = (from ve in data.VehicleInformations select ve).ToList();
            if (ModelState.IsValid)
            {
                return View(ver);
            }

            return View();
        }

        //
        // GET: /Admin/Vehicle/Details/5

        public ActionResult Details(int id)
        {
            VehicleInformation ver=data.VehicleInformations.Where(v=>v.vehicleID==id).First();
            if (ModelState.IsValid)
            {
                return View(ver);
            }

            return View();
        }

        //
        // GET: /Admin/Vehicle/Create

        public ActionResult Create()
        {
                List<Manufacturer> listManufac = (from Manufac in data.Manufacturers select Manufac).ToList();
                ViewData["listManufac"] = new SelectList(listManufac, "ManufacturerId", "Name");
                VehicleInformation ver = new VehicleInformation();
            return View();
        } 

        //
        // POST: /Admin/Vehicle/Create

        [HttpPost]
        public ActionResult Create(VehicleInformation ver)
        {
            if (ModelState.IsValid)
            {
                List<Manufacturer> listManufac = (from Manufac in data.Manufacturers select Manufac).ToList();
                ViewData["listManufac"] = new SelectList(listManufac, "ManufacturerId", "Name");

                data.VehicleInformations.InsertOnSubmit(ver);
                data.SubmitChanges();
                RedirectToAction("Index");

            }
                return View();
            
        }
        
        //
        // GET: /Admin/Vehicle/Edit/5
 
        public ActionResult Edit(int id)
        {
            List<Manufacturer> listManufac = (from Manufac in data.Manufacturers select Manufac).ToList();
            ViewData["listManufac"] = new SelectList(listManufac, "ManufacturerId", "Name");

            VehicleInformation vh = new VehicleInformation();
            return View(vh);
        }

        //
        // POST: /Admin/Vehicle/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, VehicleInformation ver)
        {
            if (ModelState.IsValid)
            {

                List<Manufacturer> listManufac = (from Manufac in data.Manufacturers select Manufac).ToList();
                ViewData["listManufac"] = new SelectList(listManufac, "ManufacturerId", "Name");

                VehicleInformation ve = data.VehicleInformations.Where(v => v.vehicleID == id).First();
                ve.vehicleName = ver.vehicleName;
                ve.vehicleisOwnerName = ver.vehicleisOwnerName;
                ve.vehicleModel = ver.vehicleModel;
                ve.vehicleVersion = ver.vehicleVersion;
                ve.vehicleRate = ver.vehicleRate;
                ve.vehicleBodyNumber = ver.vehicleBodyNumber;
                ve.vehicleEngineNumber = ver.vehicleEngineNumber;
                ve.vehicleNumber = ver.vehicleNumber;
                ve.vehicleWarranty = ver.vehicleWarranty;
                ve.Manufacturer = ver.Manufacturer;

                data.SubmitChanges();
                RedirectToAction("Index");

            }
            return View();
        }

        public ActionResult Search()
        {
            List<VehicleInformation> vh = new List<VehicleInformation>();
            return View(vh);
        }


        public ActionResult Search(string name)
        {
            List<VehicleInformation> vhs = (from vh in data.VehicleInformations.Where(v => v.vehicleName.Contains(name)) select vh).ToList();
            return View(vhs);

        }

    }
}
